<template>
	<view class="table-detail-container">
		<!-- 页面标题 -->
		<view class="page-header">
			<text class="page-title">水产收购磅码单</text>
			<text class="page-subtitle">请上传原照片作为凭证</text>
		</view>

		<!-- 照片上传区域 -->
		<view class="photo-upload-section">
			<view class="upload-title">
				<text class="title-text">凭证照片</text>
				<text class="title-desc">请上传磅码单原照片</text>
			</view>

			<view class="photo-grid">
				<view class="photo-item" v-for="(photo, index) in uploadedPhotos" :key="index">
					<image class="photo-image" :src="photo" mode="aspectFill" @click="previewPhoto(photo)" />
					<view class="photo-delete" @click="deletePhoto(index)">
						<text class="delete-icon">×</text>
					</view>
				</view>

				<view class="upload-btn" @click="choosePhoto" v-if="uploadedPhotos.length < 9">
					<view class="upload-icon">
						<text class="camera-icon">📷</text>
					</view>
					<text class="upload-text">添加照片</text>
				</view>
			</view>
		</view>

		<!-- 总数输入区域 -->
		<view class="total-input-section">
			<view class="input-title">
				<text class="title-text">总数统计</text>
				<text class="title-desc">请输入磅码单上的总数</text>
			</view>

			<view class="input-grid">
				<view class="input-item">
					<text class="input-label">总重(市斤)</text>
					<input class="total-input" type="number" v-model="totals.grossWeight" placeholder="请输入总重" />
				</view>

				<view class="input-item">
					<text class="input-label">皮重</text>
					<input class="total-input" type="number" v-model="totals.tareWeight" placeholder="请输入皮重" />
				</view>

				<view class="input-item">
					<text class="input-label">净重(市斤)</text>
					<input class="total-input" type="number" v-model="totals.netWeight" placeholder="请输入净重" />
				</view>

				<view class="input-item">
					<text class="input-label">单价</text>
					<input class="total-input" type="number" v-model="totals.unitPrice" placeholder="请输入单价"
						step="0.01" />
				</view>

				<view class="input-item">
					<text class="input-label">鱼款(元)</text>
					<input class="total-input" type="number" v-model="totals.totalAmount" placeholder="请输入鱼款" />
				</view>
			</view>
		</view>

		<!-- 底部按钮 -->
		<view class="bottom-btns">
			<button class="bottom-btn secondary" @click="prevStep">上一步</button>
			<button class="bottom-btn primary" @click="nextStep">下一步</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				type: 0, // 0，塘头  1，工厂
				uploadedPhotos: [], // 上传的照片
				totals: {
					grossWeight: '', // 总重(市斤)
					tareWeight: '', // 皮重
					netWeight: '', // 净重(市斤)
					unitPrice: '', // 单价
					totalAmount: '' // 鱼款(元)
				}
			}
		},
		onLoad(res) {
			console.log("---onLoad--->" + JSON.stringify(res))
			this.type = res.type;
		},
		methods: {
			/**
			 * 选择照片
			 */
			choosePhoto() {
				uni.chooseImage({
					count: 9 - this.uploadedPhotos.length, // 最多9张
					sizeType: ['original', 'compressed'],
					sourceType: ['album', 'camera'],
					success: (res) => {
						console.log('选择照片成功:', res)
						this.uploadedPhotos = [...this.uploadedPhotos, ...res.tempFilePaths]
						uni.showToast({
							title: '照片添加成功',
							icon: 'success'
						})
					},
					fail: (err) => {
						console.error('选择照片失败:', err)
						uni.showToast({
							title: '选择照片失败',
							icon: 'none'
						})
					}
				})
			},

			/**
			 * 预览照片
			 */
			previewPhoto(current) {
				uni.previewImage({
					urls: this.uploadedPhotos,
					current: current
				})
			},

			/**
			 * 删除照片
			 */
			deletePhoto(index) {
				uni.showModal({
					title: '确认删除',
					content: '确定要删除这张照片吗？',
					success: (res) => {
						if (res.confirm) {
							this.uploadedPhotos.splice(index, 1)
							uni.showToast({
								title: '照片已删除',
								icon: 'success'
							})
						}
					}
				})
			},

			/**
			 * 验证输入数据
			 */
			validateInput() {
				if (this.uploadedPhotos.length === 0) {
					uni.showToast({
						title: '请至少上传一张凭证照片',
						icon: 'none'
					})
					return false
				}

				if (!this.totals.grossWeight) {
					uni.showToast({
						title: '请输入总重',
						icon: 'none'
					})
					return false
				}

				if (!this.totals.netWeight) {
					uni.showToast({
						title: '请输入净重',
						icon: 'none'
					})
					return false
				}

				if (!this.totals.unitPrice) {
					uni.showToast({
						title: '请输入单价',
						icon: 'none'
					})
					return false
				}

				if (!this.totals.totalAmount) {
					uni.showToast({
						title: '请输入鱼款',
						icon: 'none'
					})
					return false
				}

				return true
			},

			/**
			 * 保存数据
			 */
			saveData() {
				const data = {
					type: this.type,
					photos: this.uploadedPhotos,
					totals: this.totals,
					timestamp: new Date().getTime()
				}

				// 保存到本地存储
				uni.setStorageSync('poundSlipData', data)
				console.log('磅码单数据已保存:', data)
			},

			prevStep() {
				uni.navigateBack()
			},

			nextStep() {
				// 验证输入
				if (!this.validateInput()) {
					return
				}

				// 保存数据
				this.saveData()

				// 跳转到下一步
				if (this.type == 0) {
					uni.navigateTo({
						url: '/pages/home/botou/summary-detail'
					})
				} else {
					uni.navigateTo({
						url: '/pages/home/gongchang/sample'
					})
				}
			}
		}
	}
</script>

<style scoped lang="scss">
	@import '@/uni.scss';

	.table-detail-container {
		background: $bg-secondary;
		min-height: 100vh;
		padding-bottom: 120rpx;
	}

	/* 页面标题 */
	.page-header {
		background: linear-gradient(135deg, $primary 0%, $secondary 100%);
		padding: 60rpx 30rpx 40rpx 30rpx;
		text-align: center;
		color: $text-white;
	}

	.page-title {
		display: block;
		font-size: 36rpx;
		font-weight: 700;
		margin-bottom: 12rpx;
		text-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.3);
	}

	.page-subtitle {
		display: block;
		font-size: 24rpx;
		opacity: 0.9;
		font-weight: 400;
	}

	/* 照片上传区域 */
	.photo-upload-section {
		background: $bg-primary;
		margin: 20rpx 30rpx;
		border-radius: $radius-xl;
		padding: 30rpx;
		box-shadow: $shadow-md;
	}

	.upload-title {
		margin-bottom: 30rpx;
	}

	.title-text {
		display: block;
		font-size: $font-lg;
		font-weight: 600;
		color: $text-primary;
		margin-bottom: 8rpx;
	}

	.title-desc {
		display: block;
		font-size: $font-sm;
		color: $text-secondary;
	}

	.photo-grid {
		display: grid;
		grid-template-columns: repeat(3, 1fr);
		gap: 20rpx;
	}

	.photo-item {
		position: relative;
		aspect-ratio: 1;
		border-radius: $radius-lg;
		overflow: hidden;
		box-shadow: $shadow-sm;
	}

	.photo-image {
		width: 100%;
		height: 100%;
		background: $bg-secondary;
	}

	.photo-delete {
		position: absolute;
		top: 8rpx;
		right: 8rpx;
		width: 32rpx;
		height: 32rpx;
		background: rgba(255, 0, 0, 0.8);
		border-radius: 50%;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.delete-icon {
		color: $text-white;
		font-size: 20rpx;
		font-weight: bold;
	}

	.upload-btn {
		aspect-ratio: 1;
		border: 2rpx dashed $border-light;
		border-radius: $radius-lg;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		background: rgba(30, 136, 229, 0.05);
		transition: all 0.3s ease;
	}

	.upload-btn:active {
		background: rgba(30, 136, 229, 0.1);
		border-color: $primary;
	}

	.upload-icon {
		margin-bottom: 8rpx;
	}

	.camera-icon {
		font-size: 40rpx;
	}

	.upload-text {
		font-size: $font-sm;
		color: $text-secondary;
	}

	/* 总数输入区域 */
	.total-input-section {
		background: $bg-primary;
		margin: 20rpx 30rpx;
		border-radius: $radius-xl;
		padding: 30rpx;
		box-shadow: $shadow-md;
	}

	.input-title {
		margin-bottom: 30rpx;
	}

	.input-grid {
		display: grid;
		grid-template-columns: repeat(2, 1fr);
		gap: 20rpx;
	}

	.input-item {
		display: flex;
		flex-direction: column;
	}

	.input-label {
		font-size: $font-sm;
		color: $text-secondary;
		margin-bottom: 12rpx;
		font-weight: 500;
	}

	.total-input {
		height: 80rpx;
		border: 2rpx solid $border-light;
		border-radius: $radius-lg;
		padding: 0 20rpx;
		font-size: $font-md;
		background: $bg-primary;
		transition: all 0.3s ease;
	}

	.total-input:focus {
		border-color: $primary;
		box-shadow: 0 0 0 4rpx rgba(30, 136, 229, 0.1);
	}

	.total-input::placeholder {
		color: $text-tertiary;
	}

	/* 底部按钮 */
	.bottom-btns {
		display: flex;
		justify-content: space-between;
		position: fixed;
		left: 30rpx;
		right: 30rpx;
		bottom: 30rpx;
		z-index: 10;
		gap: 20rpx;
	}

	.bottom-btn {
		flex: 1;
		height: 88rpx;
		border: none;
		border-radius: $radius-xl;
		font-size: $font-md;
		font-weight: 600;
		transition: all 0.3s ease;
		box-shadow: $shadow-md;
	}

	.bottom-btn.secondary {
		background: rgba(255, 255, 255, 0.9);
		color: $text-secondary;
		border: 2rpx solid $border-light;
	}

	.bottom-btn.secondary:active {
		background: rgba(255, 255, 255, 1);
		transform: translateY(2rpx);
	}

	.bottom-btn.primary {
		background: linear-gradient(135deg, $primary 0%, $secondary 100%);
		color: $text-white;
		box-shadow: 0 8rpx 24rpx rgba(51, 136, 255, 0.3);
	}

	.bottom-btn.primary:active {
		transform: translateY(2rpx);
		box-shadow: 0 4rpx 12rpx rgba(51, 136, 255, 0.4);
	}

	/* 响应式设计 */
	@media screen and (max-width: 600rpx) {
		.photo-grid {
			grid-template-columns: repeat(2, 1fr);
		}

		.input-grid {
			grid-template-columns: 1fr;
		}

		.page-header {
			padding: 40rpx 20rpx 30rpx 20rpx;
		}

		.photo-upload-section,
		.total-input-section {
			margin: 15rpx 20rpx;
			padding: 20rpx;
		}

		.bottom-btns {
			left: 20rpx;
			right: 20rpx;
			bottom: 20rpx;
		}
	}
</style>